#include "UTHNumber.h"

namespace phnq
{
	namespace uth
	{
		UTHNumber::UTHNumber(int num) : UTHValue(UTH_VALUE_TYPE_NUMBER)
		{
			this->type = UTH_NUMBER_TYPE_INT;
			this->value.intVal = num;
		}

		UTHNumber::UTHNumber(double num) : UTHValue(UTH_VALUE_TYPE_NUMBER)
		{
			this->type = UTH_NUMBER_TYPE_DOUBLE;
			this->value.doubleVal = num;
		}

		UTHNumber::~UTHNumber()
		{
		}

		bool UTHNumber::isInt()
		{
			return this->type == UTH_NUMBER_TYPE_INT;
		}
	
		bool UTHNumber::isDouble()
		{
			return this->type == UTH_NUMBER_TYPE_DOUBLE;
		}
	
		int UTHNumber::intValue()
		{
			return isInt() ? this->value.intVal : (int)this->value.doubleVal;
		}
	
		double UTHNumber::doubleValue()
		{
			return isDouble() ? this->value.doubleVal : (double)this->value.intVal;
		}

		bool UTHNumber::equals(UTHValue *val)
		{
			if(!val->isNumber())
				return false;
		
			UTHNumber *num = (UTHNumber*)val;
		
			if(num->isInt() && isInt() && num->intValue() == intValue())
				return true;
			else if(num->isDouble() && isDouble() && num->doubleValue() == doubleValue())
				return true;
			else
				return false;
		}
	
		int UTHNumber::hash()
		{
			return intValue();
		}
	
		void UTHNumber::serialize(stringbuf *sb)
		{
			char buff[80];
			if(isInt())
				sprintf(buff, "%i", intValue());
			else if(isDouble())
				sprintf(buff, "%0.16g", doubleValue());
			else
				return;
		
			sb->sputn(buff, strlen(buff));
		}
	}
}
